//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package Snake;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class SnakeGame extends JFrame {
    JPanel jp;
    Grid food = new Grid();
    Snake snake = new Snake();
    int score = 0;

    public static void main(String[] args) {
        new SnakeGame();
    }

    public SnakeGame() throws HeadlessException {
        this.init();
        this.startTimer();
        this.setKeyListener();
        this.setVisible(true);
    }

    private void setKeyListener() {
        this.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                SnakeGame.this.snake.setDirection(e.getKeyCode());
            }
        });
    }

    private void startTimer() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                Grid last = (Grid)SnakeGame.this.snake.getBody().getLast();
                SnakeGame.this.snake.move();
                if (SnakeGame.this.snake.eat(SnakeGame.this.food)) {
                    SnakeGame.this.snake.getBody().addLast(last);
                    SnakeGame.this.food.randomXY();
                    ++SnakeGame.this.score;
                }

                if (!SnakeGame.this.snake.isAlive()) {
                    System.out.println("蛇挂了");
                    int result = JOptionPane.showConfirmDialog(SnakeGame.this, "得分是" + SnakeGame.this.score + ",是否重新开始?", "温馨提示", 0);
                    System.out.println(result);
                    if (result == 0) {
                        SnakeGame.this.snake = new Snake();
                        SnakeGame.this.food.randomXY();
                        SnakeGame.this.score = 0;
                    } else {
                        System.exit(0);
                    }
                }

                SnakeGame.this.jp.repaint();
            }
        }, 0L, 100L);
    }

    private void init() {
        this.setSize(597, 400);
        this.setLocationRelativeTo((Component)null);
        this.setTitle("贪吃蛇");
        this.setDefaultCloseOperation(3);
        this.setResizable(false);
        this.food.randomXY();
        this.jp = new JPanel() {
            protected void paintComponent(Graphics g) {
                g.clearRect(0, 0, 600, 400);

                int i;
                for(i = 0; i < 37; ++i) {
                    g.drawLine(0, 1 + 10 * i, 580, 1 + 10 * i);
                }

                for(i = 0; i < 59; ++i) {
                    g.drawLine(i * 10, 1, i * 10, 361);
                }

                g.fillRect(SnakeGame.this.food.getX(), SnakeGame.this.food.getY(), 10, 10);
                g.setColor(Color.RED);
                Grid head = SnakeGame.this.snake.getHead();
                g.fillRect(head.getX(), head.getY(), 10, 10);
                g.setColor(Color.GREEN);
                LinkedList<Grid> bodys = SnakeGame.this.snake.getBody();

                for(int ix = 1; ix < bodys.size(); ++ix) {
                    Grid grid = (Grid)bodys.get(ix);
                    g.fillRect(grid.getX(), grid.getY(), 10, 10);
                }

            }
        };
        this.add(this.jp);
    }
}
